// pages/rand_color_set/rand_color_set.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_name: "page_rand_color_set",
    full_screen_color: app.globalData.full_screen_color,
    front_color: app.globalData.front_color,
    box_color_list: [],
    err_select_list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.pre_load()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  pre_load() {
    var that = this
    var remote_ip = app.globalData.remote_ip
    wx.request({
      url: remote_ip + this.data.page_name,
      header: {
        'content-type': 'application/json',
        "uid": app.globalData.uid,
      },
      success(res) {
        const rs = res.data.data[0]

        // 设置全局数据
        getApp().globalData.full_screen_color = rs.bg_color;
        getApp().globalData.front_color = rs.front_color;
        getApp().globalData.title = rs.title;

        // 设置本页数据
        that.setData({
          'full_screen_color': rs.bg_color,
          'front_color': rs.front_color,
          'box_color_list': rs.box_color_list,
          'err_select_list': [],
        });

        // 设置页面颜色
        app.page_color_init();
      }
    })
  },

  handleClick(event) {
    const selected_color = event.target.dataset.color
    const t_color = getApp().globalData.t_color
    if (selected_color != t_color) {
      wx.showToast({
        title: '选择错误',
        icon: 'error',
        duration: 200,
      })
      var err_len = this.data.err_select_list.length
      if (err_len < 50) {
        this.data.err_select_list.push(selected_color);
      }
    } else {
      // wx.redirectTo({
      //   url: '/pages/balace/balace',
      // })
      this.handle_upload_op()
    }
  },

  handle_upload_op() {
    var r_json = JSON.stringify(this.data.err_select_list)
    
    var that = this
    var remote_ip = app.globalData.remote_ip
    wx.request({
      url: remote_ip + 'op_err_select',
      header: {
        'content-type': 'application/json',
        "uid": app.globalData.uid,
        "err_list": r_json,
      },
      success(res) {
        wx.redirectTo({
          url: '/pages/balace/balace',
        })
      }
    })
  },

})